package com.pc.tas.servlet;

import com.pc.tas.learning.trace.Trace;
import com.pc.tas.learning.trace.TraceManager;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class TraceServlet extends HttpServlet {

	private static final long serialVersionUID = -3303658326867284415L;

	public void destroy() {
		super.destroy();
	}
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);	
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		try {
			
			Trace trace = TraceManager.getTrace(request);
			TraceManager.send(trace);
			
			String jsonpcallback = request.getParameter("jsonpcallback");
			//String payload = request.getParameter("payload");
			String pageId = java.net.URLDecoder.decode(request.getParameter("pageId"),"utf-8");
			String output = jsonpcallback + "(" + pageId + ");";
			response.setContentType("text/javascript");
			PrintWriter out = response.getWriter();
			out.println(output);
			out.flush();
			out.close();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	public void init() throws ServletException {
	}

}
